package com.jic.interaction.JDVOP.service.impl;

import com.alibaba.fastjson.JSON;
import com.jic.interaction.JDVOP.common.utils.JdVopHttpUtil;
import com.jic.interaction.JDVOP.common.utils.RestResult;
import com.jic.interaction.JDVOP.model.enyity.AfsServicebyCustomerPinPage;
import com.jic.interaction.JDVOP.model.enyity.BizRefundDetailVo;
import com.jic.interaction.JDVOP.model.enyity.CompatibleServiceDetailDTO;
import com.jic.interaction.JDVOP.model.enyity.ComponentExport;
import com.jic.interaction.JDVOP.model.vo.request.*;
import com.jic.interaction.JDVOP.model.vo.request.order.OrderPayRequest;
import com.jic.interaction.JDVOP.service.AfterSaleCommodityService;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: TODO
 * @author: lixuehui
 * @date: 2020/11/16 10:18
 * @Version 1.0
 */
@Service
@Slf4j
public class AfterSaleCommodityServiceImpl  implements AfterSaleCommodityService {
    @Value("${route.JDVOP}")
    private String url;


    @Override
    public RestResult<Integer> getAvailableNumberComp(AfterSaleCommodityRequest request) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException, UnsupportedEncodingException {
        log.info("校验某订单中某商品是否可以提交售后服务，入参 = {}", JSON.toJSONString(request));
        String url = this.url + "api/invoice/getAvailableNumberComp";
        System.out.println(url);
        Map map = new HashMap();
        map.put("token",request.getToken());
        map.put("param",JSONObject.fromObject(request.getParam()).toString());

        String str = JdVopHttpUtil.doRequestPost(url, map);
        Map m = com.alibaba.fastjson.JSONObject.parseObject(str);
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println(m.toString());
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println();
        if (m.get("success").toString() != "true") {
            return RestResult.error(null, m.get("resultCode").toString(), m.get("resultMessage").toString());
        }else{
            String aa = m.get("result").toString();
            return RestResult.success(Integer.parseInt(aa), m.get("resultCode").toString(),
                    m.get("resultMessage").toString());
        }

    }

    @Override
    public RestResult<List<ComponentExport>> getCustomerExpectComp(AfterSaleCommodityRequest request) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        log.info("根据订单号、商品编号查询支持的服务类型，入参 = {}", JSON.toJSONString(request));
        String url = this.url + "api/invoice/getCustomerExpectComp";
        System.out.println(url);
        Map map = new HashMap();
        map.put("token",request.getToken());
        map.put("param",JSONObject.fromObject(request.getParam()).toString());

        String str = JdVopHttpUtil.doRequestPost(url, map);
        Map m = com.alibaba.fastjson.JSONObject.parseObject(str);
        List<ComponentExport> list = (List<ComponentExport>) com.alibaba.fastjson.JSONObject.parseObject(m.get("result").toString());
        if (m.get("success").toString() != "true"){
            return RestResult.error(null, m.get("resultCode").toString(), m.get("resultMessage").toString());
        }
        return RestResult.success(list, m.get("resultCode").toString(), m.get("resultMessage").toString());

    }

    @Override
    public RestResult<List<ComponentExport>> getWareReturnJdComp(AfterSaleCommodityRequest request) {
        log.info("根据订单号、商品编号查询支持的商品返回京东方式，入参 = {}", JSON.toJSONString(request));
        String  token = "";
        String  s = url+"token="+token+"&param="+ JSONObject.fromObject(request);
        List<ComponentExport> componentExport = null;

        return RestResult.success(componentExport,"0","接口还在完善中。。。。。");
    }

    @Override
    public String createAfsApply(CreateAfsApplyRequest request) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        log.info("发起售后申请 ，入参 = {}", JSON.toJSONString(request));
        String url = this.url +"api/afterSale/createAfsApply";
        request.setToken("viyDPUi1lIYWlQeREUrpG8qs6");
        System.out.println("-------"+request.getToken());
        Map<String, String> map = org.apache.commons.beanutils.BeanUtils.describe(request);
        String post = JdVopHttpUtil.doRequestPost(url,map );
        return post;
    }

    @Override
    public RestResult<String> updateSendSku(UpdateSendSkuRequest request) {
        log.info("如果商品需要逆向发往京东，当选择第三方配送时，使用此接口填写配送信息，入参 = {}", JSON.toJSONString(request));
        String  token = "";
        String  s = url+"token="+token+"&param="+ JSONObject.fromObject(request);
        return RestResult.success("null","0","接口还在完善中。。。。。");

    }

    @Override
    public RestResult<AfsServicebyCustomerPinPage> getServiceListPage(ServiceListPageRequest request) {
        log.info("查询订单下服务单汇总信息，入参 = {}", JSON.toJSONString(request));
        String  token = "";
        String  s = url+"token="+token+"&param="+ JSONObject.fromObject(request);
        AfsServicebyCustomerPinPage pinPage= null;
        return RestResult.success(pinPage,"0","接口还在完善中。。。。。");
    }

    @Override
    public RestResult<CompatibleServiceDetailDTO> getServiceDetailInfo(ServiceDetailInfoRequest request) {
        log.info("查询服务单明细信息，入参 = {}", JSON.toJSONString(request));
        String  token = "";
        String  s = url+"token="+token+"&param="+ JSONObject.fromObject(request);
        CompatibleServiceDetailDTO detailDTO = null;
        return RestResult.success(detailDTO,"0","接口还在完善中。。。。。");
    }

    @Override
    public RestResult<Boolean> auditCancel(AuditCancelRequest request) {
        log.info("取消已经生成的服务单，入参 = {}", JSON.toJSONString(request));
        String  token = "";
        String  s = url+"token="+token+"&param="+ JSONObject.fromObject(request);
        boolean flag = false;
        return RestResult.error(flag,"null","接口还在完善中。。。。。");
    }

    @Override
    public RestResult<List<BizRefundDetailVo>> getOrderPayByOrderId(OrderPayRequest request) {
        log.info("查询退款信息，入参 = {}", JSON.toJSONString(request));
        String  token = "";
        String  s = url+"token="+token+"&param="+ JSONObject.fromObject(request);
        List<BizRefundDetailVo> bizRefundDetailVos = null;
        return RestResult.success(bizRefundDetailVos,"0","接口还在完善中。。。。。");
    }
}
